package com.wjp.villagemp.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * <p>
 * 日期帮助类
 * </p>
 * @company 中国电信甘肃万维公司
 *
 * @project agro-bp
 *
 * @version V2.0
 *
 * @author lixr
 *
 * @date 2018-3-27 下午02:21:04
 *
 * @class TimeHelper
 *
 */
public class TimeHelper {
    private static Logger logger = LoggerFactory.getLogger(TimeHelper.class);

    private static String CurrentTime;

    private static String CurrentDate;

    /**
     * 得到当前的年份 返回格式:yyyy
     *
     * @return String
     */
    public static String getCurrentYear() {
        Date nowDate = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        return formatter.format(nowDate);
    }

    /**
     * 得到当前的月份 返回格式:MM
     *
     * @return String
     */
    public static String getCurrentMonth() {
        Date nowDate = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        return formatter.format(nowDate);
    }

    /**
     * 得到当前的日期 返回格式:dd
     *
     * @return String
     */
    public static String getCurrentDay() {
        Date nowDate = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        return formatter.format(nowDate);
    }
    /**
     * 得到当前的时间，精确到毫秒,共19位 返回格式:yyyy-MM-dd HH:mm:ss
     *
     * @return String
     */
    public static String getCurrentMisTime() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
        CurrentTime = formatter.format(nowDate);
        return CurrentTime;
    }

    public static String getCurrentTime() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentTime = formatter.format(nowDate);
        return CurrentTime;
    }

    public static String getCurrentCompactTime() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        CurrentTime = formatter.format(nowDate);
        return CurrentTime;
    }
    public static String getYesterdayCompactTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        CurrentTime = formatter.format(cal.getTime());
        return CurrentTime;
    }
    public static String getYesterdayCompactTimeForFileName() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentTime = formatter.format(cal.getTime());
        return CurrentTime;
    }
    /**
     * 得到当前的日期,共10位 返回格式：yyyy-MM-dd
     *
     * @return String
     */
    public static String getCurrentDate() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        CurrentDate = formatter.format(nowDate);
        return CurrentDate;
    }
    /**
     * 得到当前的日期,共10位 返回格式：yyyy-MM-dd
     *
     * @return String
     */
    public static String getCurrentDate1() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        CurrentDate = formatter.format(nowDate);
        return CurrentDate;
    }
    /**
     * 得到当月的第一天,共10位 返回格式：yyyy-MM-dd
     *
     * @return String
     */
    public static String getCurrentFirstDate() {
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
        CurrentDate = formatter.format(nowDate);
        return CurrentDate;
    }
    /**
     * 得到当月的最后一天,共10位 返回格式：yyyy-MM-dd
     *
     * @return String
     * @throws ParseException
     */
    public static String getCurrentLastDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar= null;
        try {
            Date date =formatter.parse(getCurrentFirstDate());
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH,1);
            calendar.add(Calendar.DAY_OF_YEAR, -1);
            return formatDate(calendar.getTime());
        } catch (ParseException e) {
            logger.error("获取当月的最后一天异常：" + e.getMessage());
            return "";
        }
    }


    /**
     * 常用的格式化日期
     *
     * @param date Date
     * @return String
     */
    public static String formatDate(Date date)
    {
        return formatDateByFormat(date,"yyyy-MM-dd");
    }
    /**
     * 以指定的格式来格式化日期
     *
     * @param date Date
     * @param format String
     * @return String
     */
    public static String formatDateByFormat(Date date,String format) {
        String result = "";
        if(date != null){
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                result = sdf.format(date);
            } catch(Exception ex) {
                logger.error("以指定的格式来格式化日期异常" + ex.getMessage());
            }
        }
        return result;
    }
    /**
     * 得到当前日期加上某一个整数的日期，整数代表天数 输入参数：currentdate : String 格式 yyyy-MM-dd add_day :
     * int 返回格式：yyyy-MM-dd
     */
    public static String addDay(String currentdate, int addDay) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        int year, month, day;

        try {
            year = Integer.parseInt(currentdate.substring(0, 4));
            month = Integer.parseInt(currentdate.substring(5, 7)) - 1;
            day = Integer.parseInt(currentdate.substring(8, 10));

            gc = new GregorianCalendar(year, month, day);
            gc.add(GregorianCalendar.DATE, addDay);

            return formatter.format(gc.getTime());
        } catch (Exception e) {
            logger.error("得到当前日期加上某一个整数的日期，整数代表天数异常" + e.getMessage());
            return null;
        }
    }

    /**
     * 得到当前日期加上某一个整数的日期，整数代表月数 输入参数：currentdate : String 格式 yyyy-MM-dd add_month :
     * int 返回格式：yyyy-MM-dd
     */
    public static String addMonth(String currentDate, int addMonth) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        int year, month, day;

        try {
            year = Integer.parseInt(currentDate.substring(0, 4));
            month = Integer.parseInt(currentDate.substring(5, 7)) - 1;
            day = Integer.parseInt(currentDate.substring(8, 10));

            gc = new GregorianCalendar(year, month, day);
            gc.add(GregorianCalendar.MONTH, addMonth);

            return formatter.format(gc.getTime());
        } catch (Exception e) {
            logger.error("得到当前日期加上某一个整数的日期，整数代表月数异常" + e.getMessage());
            return null;
        }
    }
    /**
     * 得到endTime比beforeTime晚几个月，整数代表月数 输入参数：endTime、beforeTime : String 格式前7位的格式为 yyyy-MM
     */
    public static int monthDiff(String beforeTime,String endTime){
        if(beforeTime==null||endTime==null){
            return 0;
        }
        int beforeYear,endYear,beforeMonth,endMonth;
        try {
            beforeYear = Integer.parseInt(beforeTime.substring(0, 4));
            endYear = Integer.parseInt(endTime.substring(0, 4));
            beforeMonth = Integer.parseInt(beforeTime.substring(5, 7)) - 1;
            endMonth = Integer.parseInt(endTime.substring(5, 7)) - 1;
            return (endYear-beforeYear)*12+(endMonth-beforeMonth);
        } catch (Exception e) {
            logger.error("得到endTime比beforeTime晚几个月，整数代表月数异常" + e.getMessage());
        }
        return 0;
    }

    /**
     * 得到当前日期加上某一个整数的分钟 输入参数：currentdatetime : String 格式 yyyy-MM-dd HH:mm:ss
     * add_minute : int 返回格式：yyyy-MM-dd HH:mm:ss
     */
    public static String addMinute(String currentDatetime, int addMinute) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(currentDatetime.substring(0, 4));
            month = Integer.parseInt(currentDatetime.substring(5, 7))-1;
            day = Integer.parseInt(currentDatetime.substring(8, 10));

            hour = Integer.parseInt(currentDatetime.substring(11, 13));
            minute = Integer.parseInt(currentDatetime.substring(14, 16));
            second = Integer.parseInt(currentDatetime.substring(17, 19));

            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(GregorianCalendar.MINUTE, addMinute);

            return formatter.format(gc.getTime());
        } catch (Exception e) {
            logger.error("得到当前日期加上某一个整数的分钟异常" + e.getMessage());
            return null;
        }
    }

    /**
     * 得到当前日期加上某一个整数的秒 输入参数：currentdatetime : String 格式 yyyy-MM-dd HH:mm:ss
     * add_second : int 返回格式：yyyy-MM-dd HH:mm:ss
     */
    public static String addSecond(String currentDatetime, int addSecond) {
        GregorianCalendar gc = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int year, month, day, hour, minute, second;

        try {
            year = Integer.parseInt(currentDatetime.substring(0, 4));
            month = Integer.parseInt(currentDatetime.substring(5, 7))-1;
            day = Integer.parseInt(currentDatetime.substring(8, 10));

            hour = Integer.parseInt(currentDatetime.substring(11, 13));
            minute = Integer.parseInt(currentDatetime.substring(14, 16));
            second = Integer.parseInt(currentDatetime.substring(17, 19));

            gc = new GregorianCalendar(year, month, day, hour, minute, second);
            gc.add(GregorianCalendar.SECOND, addSecond);

            return formatter.format(gc.getTime());
        } catch (Exception e) {
            logger.error("得到当前日期加上某一个整数的秒异常" + e.getMessage());
            return null;
        }
    }
    public static Date parseDate(String sDate) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }

    /**
     * 解析日期及时间
     *
     * @param sDateTime
     *            日期及时间字符串
     * @return 日期
     */
    public static Date parseDateTime(String sDateTime) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");

        try {
            Date date = bartDateFormat.parse(sDateTime);
            return date;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }

    /**
     * 取得一个月的天数 date:yyyy-MM-dd
     *
     * @throws ParseException
     */
    public static int getTotalDaysOfMonth(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();

        Date date = new Date();
        try {
            date = sdf.parse(strDate);
        } catch (Exception ex) {
            logger.error("取得一个月的天数异常：" + ex.getMessage());
        }
        calendar.setTime(date);
        // 月份
        int month = calendar.get(Calendar.MONTH) + 1;
        // 天数
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return day;
    }


    /**
     * 将UTC时间转换为java.util.date
     * @param str
     * @return
     */
    public static Date getDate(String str) throws ParseException {
        // 注意是空格+UTC
        str = str.replace("Z", " UTC");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        Date date = sdf.parse(str);
        return date;
    }

}
